<template>
	<view class="page">
		<view class="list">
			<view class="item">
				<text class="key">原账号</text>
				<text class="value">{{rowAccount}}</text>
			</view>
			<view class="item" @tap="editNew">
				<text class="key">新账号</text>
				<text class="value">{{newAccount}}</text>
			</view>
			<view class="register-desc">
				设置的账号只能是大陆手机号
			</view>
		</view>
		<uni-popup ref="popupNew" type="dialog">
			<uni-popup-dialog mode="input" title="新账号" :value="newAccount" placeholder="请输入新账号" @confirm="finishNew">
			</uni-popup-dialog>
		</uni-popup>
		<button class="save-btn" @tap="save">确定</button>
	</view>
</template>

<script>
	import uniPopup from '@/components/uni-popup/uni-popup.vue';
	import uniPopupMessage from '@/components/uni-popup/uni-popup-message.vue';
	import uniPopupDialog from '@/components/uni-popup/uni-popup-dialog.vue';

	export default {
		components: {
			uniPopup,
			uniPopupMessage,
			uniPopupDialog,
		},
		data() {
			return {
				rowAccount:'',
				newAccount:'',
				opt: null
			}
		},
		onShow:function(){
			let that = this;
			that.ajax(that.url.getAccount,'GET',null,function(resp){
				let data = resp.data;
				if(data.account!=null || data.account.length>0){
					that.rowAccount = data.account;
				}else{
					uni.showToast({
						title:'无法获取当前账户',
						icon:'error'
					});
				}
			});
		},
		methods: {
			editNew: function() {
				let that = this;
				if(that.rowAccount==null||that.rowAccount.length<=0){
					uni.showToast({
						title:'请获取当前账户',
						icon:'error'
					});
					return;
				}
				this.$refs.popupNew.open();
			},
			finishNew: function(done, value) {
				let that = this;
				if (!this.checkValidAccount(value, '账户')) {
					if(value === that.rowAccount){
						uni.showToast({
							title:'账户一致',
							icon:'error'
						});
						value='';
					}else{
						that.newAccount = value;
						that.opt='modified';
						done();
					}
				}
			},
			save: function() {
				let that = this;
				if (that.opt == 'modified') {
					if (that.checkValidAccount(that.newAccount, '账户')) {
						return;
					}
					uni.showModal({
						title: '提示',
						content: '是否确认修改账号',
						success: function(e) {
							if (e.confirm) {
								that.ajax(that.url.updateAccount, 'PUT', {
									account:that.newAccount
								}, function(resp) {
									let data = resp.data;
									uni.showToast({
										icon: 'success',
										title: "修改成功",
									});
									setTimeout(function(){
										uni.removeStorageSync("token");
										uni.navigateTo({
											url: "../login/login"
										})
									},2000);
								});
							}
						}
					});
				}else{
					uni.showToast({
						icon: 'error',
						title: '请填写相关内容',
					});
				}
			}
		}
	}
</script>

<style lang="less">
	@import url("modified_account.less");
</style>
